import { getUserLog, setUserLog, removeUserLog } from '@/utils/auth'
import { empty } from '@/utils/test';
import { getConsumerInfo, consumerFault, consumerInstall } from '@/api/index.js';


/**
 * @example 用户报装报修
 * @constant {Object} log 用户报装报修记录
 */
const state = getUserLog()

const mutations = {
	/**
	 * @example 设置用户报装报修记录
	 * @param {Object} log 用户报装报修记录
	 * @returns {Object} 用户报装报修记录
	 */
	SET_LOG: (state, log) => {
		const { name, phone, address, country } = log
		setUserLog({ name, phone, address, country })
		state = { name, phone, address, country }
	},
}

const actions = {
	/**
	 * @example 登录第三步: 获取token后,拉取用户的基本信息
	 * @returns {Promise} 设置用户信息和其他缓存的基础信息
	 */
	getInfo({ commit, state, dispatch }) {
		return new Promise((resolve, reject) => {
			// 这里只能用 this._vm来获取Vue
			getConsumerInfo().then(response => {
				if (!response) {
					reject('获取可选信息失败。')
				}
				// 设置全部的基础数据
				dispatch('set_basicsState', response, {
					root: true
				})
				resolve(response)
			}).catch(error => {
				reject(error)
			})
		})
	},
	/**
	 * @example 用户报修
	 * @param {string} data 报修的信息
	 * @returns {Promise} 报修的信息
	 */
	fault({ commit }, data) {
		return new Promise((resolve, reject) => {
			consumerFault(data).then(response => {
				commit('SET_LOG', response)
				resolve(response)
			}).catch(error => {
				reject(error)
			})
		})
	},
	/**
	 * @example 用户报装
	 * @param {string} data 报装的信息
	 * @returns {Promise} 报装的信息
	 */
	install({ commit }, data) {
		return new Promise((resolve, reject) => {
			consumerInstall(data).then(response => {
				commit('SET_LOG', response)
				resolve(response)
			}).catch(error => {
				reject(error)
			})
		})
	},
}

const getters = {}
export default {
	namespaced: true,
	state,
	getters,
	mutations,
	actions
}